package cn.stylefeng.guns.modular.question.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.enums.CommonStatusEnum;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.exception.enums.StatusExceptionEnum;
import cn.stylefeng.guns.modular.User.enums.UserEnum;
import cn.stylefeng.guns.modular.question.entity.quenstionnaire;
import cn.stylefeng.guns.modular.question.mapper.questionMapper;
import cn.stylefeng.guns.modular.question.param.questionParam;
import cn.stylefeng.guns.modular.question.service.questionService;
import cn.stylefeng.guns.sys.modular.emp.service.SysEmpService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class QuestionServiceImpl extends ServiceImpl<questionMapper, quenstionnaire> implements questionService {

    @Resource
    private SysEmpService sysEmpService;

    @Resource
    private questionMapper questionMapper;


    @Override
    public List<quenstionnaire> list(questionParam questionParam) {
        LambdaQueryWrapper<quenstionnaire> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(quenstionnaire::getStatus, CommonStatusEnum.DELETED.getCode());
        return this.list(queryWrapper);
    }

    @Override
    public void add(questionParam questionParam) {
        quenstionnaire quenstionnaire = new quenstionnaire();
        BeanUtil.copyProperties(questionParam,quenstionnaire);
        this.save(quenstionnaire);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(questionParam questionParam) {
        quenstionnaire quenstionnaire = this.queryCanteenUser(questionParam);


        quenstionnaire.setStatus(CommonStatusEnum.DELETED.getCode());
        this.updateById(quenstionnaire);
        Long id = quenstionnaire.getId();
        //删除该用户对应的员工表信息
        sysEmpService.deleteEmpInfoByUserId(id);

        this.removeById(id);
    }

    @Override
    public void edit(questionParam questionParam) {
        quenstionnaire quenstionnaire = this.queryCanteenUser(questionParam);
        BeanUtil.copyProperties(questionParam,quenstionnaire);
        quenstionnaire.setStatus(null);
        this.updateById(quenstionnaire);
    }

    @Override
    public void changeStatus(questionParam questionParam) {
        Long id = questionParam.getId();
        Integer status = questionParam.getStatus();

        // 校验状态在不在枚举值里
        CommonStatusEnum.validateStatus(status);

        // 更新枚举，更新只能更新未删除状态的
        LambdaUpdateWrapper<quenstionnaire> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
                .eq(quenstionnaire::getId,id)
                .and(i->i.ne(quenstionnaire::getStatus,CommonStatusEnum.DELETED.getCode()))
                .set(quenstionnaire::getStatus,status);
        boolean update = this.update(updateWrapper);
        if (!update) {
            throw new ServiceException(StatusExceptionEnum.UPDATE_STATUS_ERROR);
        }
    }

    private quenstionnaire queryCanteenUser(questionParam questionParam) {
        quenstionnaire quenstionnaire = this.getById(questionParam.getId());
        if (ObjectUtil.isEmpty(quenstionnaire)) {
            throw new ServiceException(UserEnum.NOT_EXISTED);
        }
        return quenstionnaire;

    }
}
